﻿using AutoMapper;
using MediatR;
using Traceability.API.Applications.Command.PictureCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.PictureCommandHandler
{
    public class PictureDelCommandHandler : IRequestHandler<PictureDelCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Picture> _pictureRepository;
      

        public PictureDelCommandHandler(IBaseRepository<Picture> pictureRepository)
        {
            _pictureRepository = pictureRepository;
           
        }
        public async Task<APIResult<int>> Handle(PictureDelCommand request, CancellationToken cancellationToken)
        {
           var res= _pictureRepository.GetAll().FirstOrDefault(s=>s.Id==request.Id);
            int count=await _pictureRepository.Del(res);

            APIResult<int> aPIResult = new APIResult<int>();
            if (count > 0)
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Data = count;
                aPIResult.Msg = "删除成功";
            }
            else
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Data = count;
                aPIResult.Msg = "删除失败";
            }
            return await Task.FromResult(aPIResult);



        }
    }
}
